﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>书籍列表</title>
    <link rel="stylesheet" type="text/css" href="/manager/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="/manager/easyui/themes/icon.css">
    <script type="text/javascript" src="/manager/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="/manager/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript">
        //工具栏
        var toolbar = [{
            text: '修改',
            iconCls: 'icon-edit',
            handler: function () {
                $('#dd').dialog('open');
                let row = $("#booklist").datagrid("getSelected");
                $("#show-book-id").val(row.id);

                $.post("/manager/book/findOne", {id: row.id}, rs => {
                    console.log(rs);
                    $("#show-book-name").textbox("setValue", rs.data.bookname);
                    $("#show-book-price").textbox("setValue", rs.data.price);
                    $("#show-book-group-price").textbox("setValue", rs.data.groupprice);
                    $("#show-book-bookintroduce").textbox("setValue", rs.data.bookintroduce);
                    $("#show-book-authorintroduce").textbox("setValue", rs.data.authorintroduce);

                    let imgurl = rs.data.imgurl;
                    imgurl = imgurl.indexOf("http") == 0 ? imgurl : "/manager/" + imgurl;
                    $("#show-img-id").attr("src", imgurl);
                })
            }
        }, '-', {
            text: '启用',
            iconCls: 'icon-ok',
            handler: function () {
                let row = $("#booklist").datagrid("getSelected");
                $.ajax({
                    url: '/manager/book/update',
                    type: "post",
                    data: {
                        id: row.id,
                        flag: 1,
                    },
                    success: function (data) {
                        $('#booklist').datagrid("reload");
                    }
                });
            }
        }, '-', {
            text: '禁用',
            iconCls: 'icon-cancel',
            handler: function () {
                let row = $("#booklist").datagrid("getSelected");
                $.ajax({
                    url: '/manager/book/update',
                    type: "post",
                    data: {
                        id: row.id,
                        flag: 0,
                    },
                    success: function (data) {
                        $('#booklist').datagrid("reload");
                    }
                });
            }
        }];
        $(function () {
            $('#booklist').datagrid({
                title: '书籍列表',
                iconCls: 'icon-save',
                url: '/manager/book/examineFindAll',
                columns: [
                    [
                        {field: 'bookname', title: '名称', width: 100, align: 'center'},
                        {field: 'author', title: '作者', width: 200, align: 'center'},
                        {field: 'price', title: '单价', width: 80, align: 'center'},
                        {field: 'isbn', title: 'ISBN', width: 200, align: 'center'},
                        {
                            field: 'press', title: '出版社', width: 200, align: 'center',
                            formatter: function (value, row, index) {
                                return value !=null ? value.name : null;
                            }
                        },
                        {
                            field: 'suppliers', title: '供货商', width: 500, align: 'center',
                            formatter: function (value, row, index) {
                                let rs = "";
                                for (const supplier of value) {
                                    rs += supplier.name + "; ";
                                    console.log(rs);
                                }
                                return rs;
                            }
                        },
                        {
                            field: 'flag',
                            title: '状态',
                            width: 100,
                            align: 'center',
                            formatter: function (value, row, index) {
                                return value == "1" ? "启用" : "禁用";
                            }
                        }
                    ]],
                toolbar: toolbar,
                pagination: true,//表示在datagrid设置分页
                pageSize: 5,
                singleSelect: true,
                pageList: [5, 10, 20]
            });

            //初始化查询栏
            $("#submit_search").linkbutton({iconCls: 'icon-search', plain: true})
                .click(function () {
                    let data = {};
                    let bookname = $("#search-book-name").val();
                    let isbn = $("#search-book-isbn").val();
                    let supplierId = $("#search-book-supplier-id").val();
                    let pressid = $("#search-book-press-id").val();
                    if (bookname != "") {
                        data.bookname = bookname;
                    }
                    if (isbn != "") {
                        data.isbn = isbn;
                    }
                    if (supplierId != "-1") {
                        data.supplierId = supplierId;
                    }
                    if (pressid != "-1") {
                        data.pressid = pressid;
                    }

                    $('#booklist').datagrid("reload", data);
                });

            //初始化修改窗口
            $('#dd').dialog({
                title: 'My Dialog',
                width: 700,
                height: 500,
                closed: true,
                cache: false,
                href: '',
                modal: true,
                iconCls: 'icon-save',
                buttons: [{
                    text: '提交',
                    iconCls: 'icon-ok',
                    handler: function () {
                        let data = new FormData($("#ff")[0]);
                        console.log(data);
                        $.ajax({
                            url: "/manager/book/update",
                            type: "post",
                            data: data,
                            // dataType: "json",
                            processData: false,
                            contentType: false,
                            success: function (rs) {
                                $('#booklist').datagrid("reload");
                            }
                        });
                    }
                }, {
                    text: '关闭',
                    handler: function () {
                        $('#dd').dialog("close");
                    }
                }]
            });


            //获取供应商数据
            $.post("/manager/supplier/findAll", rs => {
                $("#search-book-supplier-id").append("<option value=\"-1\">请选择供应商</option>");
                console.log(rs.data.length);
                console.log(rs);
                for (const r of rs.data) {
                    $("#search-book-supplier-id").append("<option value=" + r.id + ">" + r.name + "</option>");
                }
            });

            //获取出版社数据
            $.post("/manager/press/findAll", rs => {
                $("#search-book-press-id").append("<option value=\"-1\">请选择出版社</option>");
                for (const r of rs.data) {
                    $("#search-book-press-id").append("<option value=" + r.id + ">" + r.name + "</option>");
                }
            });
        });
    </script>
</head>
<body>
<form name="searchform" method="post" action="" id="searchform">
    <td width="70" height="30"><strong>书籍查询：</strong></td>
    <td height="30">
        <input type="text" name="keyword" id="search-book-name" size=20 placeholder="书籍名称"/>
        <input type="text" name="keyword" id="search-book-isbn" size=20 placeholder="ISBN"/>
        <select name="pressid" id="search-book-press-id">
        </select>
        <select name="supplierid" id="search-book-supplier-id">
        </select>
        <!--        编辑推荐:<input type="checkbox" name="" value=""/>-->
        <!--        本周精选:<input type="checkbox" name="" value=""/>-->
    </td>
    <td>
        <a id="submit_search">搜索</a>
    </td>
</form>
<table id="booklist"></table>
<div id="dd">
    <div align="center" style="margin-top: 30px">
        <form id="ff" method="post">
            <input name="id" type="hidden" id="show-book-id">
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="bookname" style="width:50%"
                       id="show-book-name"
                       data-options="label:'书籍名称:',required:true">
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="price" style="width:50%"
                       id="show-book-price"
                       data-options="label:'单价:',required:true">
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="groupprice" style="width:50%"
                       id="show-book-group-price"
                       data-options="label:'团购价:',required:true">
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="bookintroduce" style="width:50%;height:60px"
                       id="show-book-bookintroduce"
                       data-options="label:'书籍简介:',multiline:true">
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="authorintroduce" style="width:50%;height:60px"
                       id="show-book-authorintroduce"
                       data-options="label:'作者简介:',multiline:true">
            </div>
            <div style="margin-bottom:20px">
                封面图片:&nbsp;&nbsp;&nbsp;&nbsp; <input type="file" name="img"/>
            </div>

            <div style="margin-bottom:20px">
                <img id="show-img-id" alt="没有对应图片" src=""></img>
            </div>
            <!--            <div style="margin-bottom:20px">-->
            <!--                编辑推荐:<input type="checkbox" name="" value=""/>-->
            <!--                本周精选:<input type="checkbox" name="" value=""/>-->
            <!--            </div>-->

        </form>
    </div>
</div>
</body>
</html>